package com.cirkana.serial;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public abstract class SerializablePassData {

	public abstract void serialize(DataOutputStream out) throws IOException;

	public abstract void deserialize(DataInputStream in) throws IOException;

	protected void serializeBoolean(boolean bool, DataOutputStream out)
			throws IOException {
		out.writeBoolean(bool);
	}

	protected boolean deserializeBoolean(DataInputStream in) throws IOException {
		return in.readBoolean();
	}

	protected void serializeString(String string, DataOutputStream out)
			throws IOException {
		if (string == null)
			serializeBoolean(true, out);
		else {
			serializeBoolean(false, out);
			out.write(string.length());
			out.writeChars(string);
		}
	}

	protected String deserializeString(DataInputStream in) throws IOException {
		if (deserializeBoolean(in))
			return null;
		else {
			int length = in.readInt();
			StringBuffer s = new StringBuffer(length);
			for (int iii = 0; iii < length; iii++)
				s.append(in.readChar());
			return s.toString();
		}
	}

	protected void serializeInteger(int integer, DataOutputStream out)
			throws IOException {
		out.write(integer);
	}

	protected int deserializeInteger(DataInputStream in) throws IOException {
		return in.readInt();
	}
}
